﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace SuperSocketServer.Common
{
    class LogHelper
    {
        public static List<string> AllLines = new List<string>();
        public static int DisplayLength = 0;

        public static void SetOnLog()
        {
            var timer = new Timer(5000);
            timer.Elapsed += (s, x) =>
            {
                try
                {
                    int count = AllLines.Count;
                    if(DisplayLength==count) return;
                    //最多保留log行数
                    if (AllLines.Count>5000)
                    {
                        AllLines.RemoveRange(0,20);
                        DisplayLength = AllLines.Count;
                        Console.WriteLine(string .Join("\r\n", AllLines));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            };
            timer.Enabled = true;
            timer.Start();
        }
        /// <summary>
        ///  普通的文件记录日志
        /// </summary>
        /// <param name="info"></param>
        public static void WriteLog(string info)
        {

                AllLines.Add(string.Join(" ", DateTime.Now.ToString("HH:mm:ss"), info));           
        }
    }
}
